﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections;
using System.Collections.Generic;

namespace Cn.Cellula.Client.ControlHelper {
        public class GvHelper {
            public static IList<String> GetSelectedKey(ref GridView gv,String cbxId) {
                IList<String> list = new List<String>();
                if(gv.DataKeys.Count <1)return list;
                for (int i = 0; i < gv.Rows.Count; i++) {
                    object obj = gv.Rows[i].FindControl(cbxId);
                    if (obj == null || obj.GetType()!=typeof(CheckBox)) continue;
                    CheckBox chkbox = (CheckBox)obj;
                    if (chkbox.Checked && gv.DataKeys[i].Values.Count>0) {
                        list.Add(gv.DataKeys[i].Values[0].ToString());
                    }
                }
                return list;
            }
            public static void GvSelected(ref GridView gv, String Key,String cbxId) {
                if (gv.DataKeys.Count < 1) return;
                object obj = null;
                CheckBox chkbox = null;
                for (int i = 0; i < gv.Rows.Count; i++) {
                    if (!String.IsNullOrEmpty(cbxId)) {
                        obj = gv.Rows[i].FindControl(cbxId);
                        if (obj != null && obj.GetType() == typeof(CheckBox)) {
                            chkbox = (CheckBox)obj;
                        }
                    }
                    if (gv.DataKeys[i].Values[0].ToString() == Key) {
                        gv.Rows[i].CssClass = "GridViewRowSelected";
                        if (chkbox != null) 
                            chkbox.Checked = true;

                    } else {
                        gv.Rows[i].CssClass = ((i+1)%2==0)?"GirdViewAlternatingRow":"GridViewRow";
                        if (chkbox != null)
                            chkbox.Checked = false;
                    }
                }
            }
            #region 绑定GridView的下来列表翻页
            public const string GV_PAGER_DDL_ID = "DDL_PAGER_SELECTOR";
            public const string GV_TOTAL_LAB_ID = "LAB_PAGER_TOTALCOUNT";
            public const string ATTR_TOTAL_COUNT = "TOTAL_COUNT";
            public static void AppendEventHandler(ref GridView rGv){
                rGv.DataBound += new EventHandler(GvHelper.GvDdlPager_OnDataBound);
            }
            public static void GvDdlPager_OnDataBound(object sender,EventArgs e) {
                GridView gv = (GridView)sender;
                if (gv != null && gv.BottomPagerRow != null) {
                    DropDownList ctrl = (DropDownList)gv.BottomPagerRow.FindControl(GvHelper.GV_PAGER_DDL_ID);
                    if (ctrl != null) {
                        for (int i = 1; i <= gv.PageCount; i++) {
                            ctrl.Items.Add(new ListItem(i + "", i + ""));
                        }
                        ctrl.SelectedValue = (gv.PageIndex + 1) + "";
                    }

                    Label lab = (Label)gv.BottomPagerRow.FindControl(GvHelper.GV_TOTAL_LAB_ID);
                    if (lab != null) { 
                        lab.Text = gv.Attributes[GvHelper.ATTR_TOTAL_COUNT];
                    }

                    gv.BottomPagerRow.Visible = true;
                }


            }
            #endregion
        }

}
